/**
 * ipc通信处理函数
 */
import { Notification, ipcMain, nativeTheme } from "electron";
import { electronLogger } from "../logger/logger";
import os from "os";
import { readConfig } from "./configManager";

const logger = electronLogger;

// 登录
ipcMain.on(`login`, async (event, username: string, password: string) => {
    console.log(666)
    console.log(username, password)
    // TODO: 登录
    event.reply("login", "login success")
});

// TODO: 退出登录
ipcMain.on(`logout`, async (event) => {
    event.reply("logout", "logout success")
})

// 发送系统通知
ipcMain.on(`sendNotification`, async (event, title: string, message: string) => {
    logger.info(`发送系统消息 ${title} ${message}`);
    new Notification({
        title: title,
        body: message
    }).show()
})

ipcMain.on(`doSomeThing`, async (event) => {
    console.log(`doSomeThing`);
})

// 获取系统信息
ipcMain.handle(`getOSInfo`, async (event) => {
    const osName = os.platform()
    const arch = os.arch()
    const release = os.release()
    console.log(osName, arch, release)
    return {
        osName,
        arch,
        release
    }
})

ipcMain.handle('getConfig', async () => {
    return readConfig();
});

/**
 * 获取当前系统主题
 */
ipcMain.handle("getDarkMode", () => {
    return nativeTheme.themeSource; // 'light' | 'dark' | 'system'
})

ipcMain.handle("setDarkMode", async (event, mode: "light" | "dark" | "system") => {
    nativeTheme.themeSource = mode;
    return nativeTheme.themeSource;
})